package main

import (
	"flag"
	"os"
	"runtime"
	"github.com/silenceper/log"
)

var (
	cfg         *config
	exit        chan bool
)

func init() {
	flag.StringVar(&configFile, "c", "gowatch.yml", "config file.")
}

var ignoredFilesRegExps = []string{
	`.(\w+).go.swp`,
	`(\w+).tmp`,
}

func main() {
	flag.Parse()

	// 配置文件必须存在
	_, err := os.Stat(configFile)
	if err != nil || os.IsNotExist(err) {
		log.Fatalf("config file can't find:%s", configFile)
		os.Exit(1)
	}

	cfg = parseConfig()
	runApp()
}

func runApp() {
	var paths []string
	//除了当前目录，增加额外监听的目录
	//log.Infof("config:%+v\n", cfg)
	for _, path := range cfg.WatchPaths {
		readAppDirectories(path, &paths)
	}

	NewWatcher(paths)
	for {
		select {
		case <-exit:
			runtime.Goexit()
		}
	}
}
